<?php

namespace app\admin\controller;

use think\facade\View;

use app\admin\model\Category as CategoryModel;
use app\admin\model\Article as ArticleModel;
use app\admin\model\TopicCategory as TopicCategoryModel;
use app\admin\model\TopicContent as ContentModel;
use app\admin\model\TopicSun as SunModel;
use app\admin\model\TopicInfo as InfoModel;

/**
 * 专题信息管理
 * @author huwhis@163.com
 * @version   0.0.1
 */
class TopicInfo extends Base
{
    public function index($id = 0)
    {
        $topicInfo = ContentModel::field('id,cid,title,topic_url,create_time')->find($id);

        $list = InfoModel::alias('tinfo')
            ->join('top_topic_content t', 'tinfo.tid = t.id')
            ->join('top_article a', 'tinfo.aid = a.id')
            ->join('top_category c', 'tinfo.cid = c.id')
            ->leftjoin('top_topic_sun ts', 'tinfo.tsid = ts.id')
            ->field('tinfo.id,tinfo.tid,tinfo.tsid,tinfo.tdid,tinfo.sort,t.title AS t_title,ts.title AS ts_title,a.id as aid,a.title,a.userid,a.username,a.create_time,c.name as category_name')
            ->where('tinfo.tid', $id)->order('id desc')->paginate(['query' => ['id' => $id]]);

        return View::fetch('index', [
            'list' => $list,
            'topicInfo' => $topicInfo,
        ]);
    }

    public function info($tid = 0, $id = 0)
    {
        if (!$tid) {
            $this->error('请选择专题');
        }

        if ($id) {
            $data = InfoModel::find($id);
        } else {
            $data = new InfoModel();
        }

        return View::fetch('save', [
            'tid' => $tid,
            'data' => $data,
        ]);
    }

    public function save()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();

            if (!$param['tid']) {
                $this->error('请选择专题');
            }

            try {
                InfoModel::saveAids($param['a_c_ids'], $param['tsid'], $param['tid']);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:" . $msg);
            }

            $this->success('操作成功', url('topic_sun/index', ['tid' => $param['tid']]));
        }
    }

    public function articleList($tid)
    {
        $param = $this->app->request->param();

        $cid = isset($param['cid']) ? (int) $param['cid'] : 0;
        $key = isset($param['key']) ? (string) $param['key'] : '';

        $list = ArticleModel::queryPage($param);

        $categoryTree = obj_tree(CategoryModel::field('id, pid, name')->select());

        $topicInfoAidList = InfoModel::where('tid', $tid)->column('aid');

        $topicSun = SunModel::where('tid', $tid)->select();

        return View::fetch('', [
            'tid' => $tid,
            'cid' => $cid,
            'key' => $key,
            'list' => $list,
            'categoryTree' => $categoryTree,
            'topicInfoAidList' => $topicInfoAidList,
            'topicSun' => $topicSun
        ]);
    }

    public function pushInfo()
    {
        $aids = $this->app->request->param('aids');

        $tclist = obj_tree(TopicCategoryModel::select());

        return View::fetch('', [
            'aids' => $aids,
            'tclist' => $tclist
        ]);
    }

    public function saveaid()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();

            $aids = explode(',', $param['aids']);
            $tid = $param['tid'];
            $tsid = $param['tsid'];

            $info_aid = InfoModel::where('tid', $tid)->column('aid');
            
            $info_aids = array_diff($aids, $info_aid);

            $a_c_ids = [];
            foreach ($info_aids as $aid) {
                $cid = ArticleModel::where('id', $aid)->value('cid');
                $a_c_ids[] = [
                    'aid' => $aid,
                    'cid' => $cid
                ];
            }

            try {
                InfoModel::saveAids($a_c_ids, $tsid, $tid);

                return ['code'=>0, 'msg'=>'推送成功'];
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:" . $msg);
            }
        }
    }
}
